﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApCableAttachmentFrameUpdater
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.Flexibles;
using System;
using System.ComponentModel;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands;

public class CApCableAttachmentFrameUpdater
{
  private bool m_isModifyingFixedFrame;
  private ITxLocatableObject m_lastSetAttachmentFrame;
  private readonly TxObjEditBoxControl m_attachmentFrameEditBox;
  private readonly CApCableAttachmentFrameController m_attachmentFrameController;
  private readonly ICApFLEXCableAttributeViewModel m_attributeViewModel;
  private readonly CApFLEXCableAttachmentViewModel m_attachmentViewModel;

  public CApCableAttachmentFrameUpdater(
    TxObjEditBoxControl attachmentFrameEditBox,
    CApCableAttachmentFrameController attachmentFrameController,
    ICApFLEXCableAttributeViewModel attributeViewModel,
    CApFLEXCableAttachmentViewModel attachmentViewModel)
  {
    this.m_isModifyingFixedFrame = false;
    this.m_attachmentFrameEditBox = attachmentFrameEditBox;
    this.m_attachmentFrameController = attachmentFrameController;
    this.m_attributeViewModel = attributeViewModel;
    this.m_attachmentViewModel = attachmentViewModel;
    this.m_attachmentViewModel.PropertyChanged += new PropertyChangedEventHandler(this.OnAttachmentPropertyChanged);
    this.OnAttachmentLocationChanged();
  }

  public void SetAttachmentFrame(ITxLocatableObject frame)
  {
    if (this.m_isModifyingFixedFrame)
      return;
    this.m_isModifyingFixedFrame = true;
    string name = frame == null ? "" : ((ITxObject) frame).Name;
    ITxLocatableObject fixedFrame = this.m_attachmentViewModel.FixedFrame;
    this.m_attachmentFrameController.FrameEditorPicked(frame as TxFrame, ref fixedFrame, name);
    this.m_attachmentViewModel.FixedFrame = fixedFrame;
    this.m_attachmentFrameEditBox.Text = name;
    this.UpdateLastSetAttachmentFrame();
    this.m_isModifyingFixedFrame = false;
  }

  public void UnsetAttachmentFrame()
  {
    if (this.m_isModifyingFixedFrame)
      return;
    this.m_isModifyingFixedFrame = true;
    this.m_attachmentFrameController.AttachmentParentFrameDidChanged(this.m_attachmentViewModel.FixedFrame);
    CApCableUtility.UnHighlightObject((ITxDisplayableObject) (this.m_attachmentViewModel.FixedFrame as TxFrame));
    this.m_attachmentViewModel.FixedFrame = (ITxLocatableObject) null;
    this.m_attachmentFrameEditBox.Text = "";
    this.UpdateLastSetAttachmentFrame();
    this.m_isModifyingFixedFrame = false;
  }

  public void UnregisterEvents()
  {
    this.m_attachmentViewModel.PropertyChanged -= new PropertyChangedEventHandler(this.OnAttachmentPropertyChanged);
  }

  private void TrySetLastAttachmentFrame()
  {
    if (this.m_lastSetAttachmentFrame == null || !((ITxObject) this.m_lastSetAttachmentFrame).IsValid())
      this.UnsetAttachmentFrame();
    else
      this.SetAttachmentFrame(this.m_lastSetAttachmentFrame);
  }

  private void OnAttachmentLocationChanged()
  {
    switch (this.m_attachmentViewModel.Location)
    {
      case CApFLEXAttachmentLocation.AlongCable:
        this.TrySetLastAttachmentFrame();
        break;
      case CApFLEXAttachmentLocation.Start:
        this.SetAttachmentFrame(this.m_attributeViewModel.StartFrame);
        break;
      case CApFLEXAttachmentLocation.End:
        this.SetAttachmentFrame(this.m_attributeViewModel.EndFrame);
        break;
      default:
        throw new NotImplementedException();
    }
  }

  private void OnFixedFrameChanged()
  {
    this.SetAttachmentFrame(this.m_attachmentViewModel.FixedFrame);
  }

  private void OnAttachmentPropertyChanged(object sender, PropertyChangedEventArgs e)
  {
    if (e.PropertyName == "Location")
    {
      this.OnAttachmentLocationChanged();
    }
    else
    {
      if (!(e.PropertyName == "FixedFrame"))
        return;
      this.OnFixedFrameChanged();
    }
  }

  private void UpdateLastSetAttachmentFrame()
  {
    if (this.m_attachmentViewModel.Location != CApFLEXAttachmentLocation.AlongCable)
      return;
    this.m_lastSetAttachmentFrame = this.m_attachmentViewModel.FixedFrame;
  }
}
